package com.mygdx.game.systems;


import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
import com.mygdx.game.components.MovementComponent;
import com.mygdx.game.components.PositionComponent;

/**
 * 玩家控制系统
 * Created by Minus K on 17/3/20.
 */
public class PlayerControlSystem extends IteratingSystem {
    private ComponentMapper<PositionComponent> pm = ComponentMapper.getFor(PositionComponent.class);
    private ComponentMapper<MovementComponent> mm = ComponentMapper.getFor(MovementComponent.class);

    public PlayerControlSystem() {
        super(Family.all(PositionComponent.class, MovementComponent.class).get());
    }

    @Override
    public void processEntity(Entity entity, float deltaTime) {
        PositionComponent position = pm.get(entity);
        MovementComponent movement = mm.get(entity);

        if (Gdx.input.isKeyPressed(59)) {
            if (Gdx.input.isKeyPressed(19)) {
                position.y += movement.velocityY + 2.0f;
            }
            if (Gdx.input.isKeyPressed(20)) {
                position.y -= movement.velocityY + 2.0f;
            }
            if (Gdx.input.isKeyPressed(21)) {
                position.x -= movement.velocityX + 2.0f;
            }
            if (Gdx.input.isKeyPressed(22)) {
                position.x += movement.velocityX + 2.0f;
            }
        } else {
            if (Gdx.input.isKeyPressed(19)) {
                position.y += movement.velocityY + 5.0f;
            }
            if (Gdx.input.isKeyPressed(20)) {
                position.y -= movement.velocityY + 5.0f;
            }
            if (Gdx.input.isKeyPressed(21)) {
                position.x -= movement.velocityX + 5.0f;
            }
            if (Gdx.input.isKeyPressed(22)) {
                position.x += movement.velocityX + 5.0f;
            }
        }
        if (Gdx.input.isTouched()) {
            position.x += movement.velocityX + Gdx.input.getDeltaX();
            position.y -= movement.velocityY + Gdx.input.getDeltaY();
        }
    }
}
